// pages/shopCart/shopCart.js
import {navigate} from '../../loginToken/loginToken.js'
import {
        getread,
        getupdata,
        getnumbers,
        getremove
} from '../../netWork/api/apiCart.js';
Page({
        /**
         * 页面的初始数据
         */
        data: {
                carts: [], //数据
                mack: true,
                price: "0.00", //价格
                selected: null,
        },

        // 总价
        price() {
                let price = 0;
                let arr = this.data.carts.filter(item => {
                        return item.selected == true;
                })
                arr.forEach(item => {
                        price += item.number * item.price
                });
                this.setData({
                        price: price
                })
                // 计算总价
        },

        // 复选框的状态
        check(e) {
                let {
                        selected,
                        key
                } = e.currentTarget.dataset
                let obj = {
                        selected: !selected,
                        key
                }
                getupdata(obj).then(res => {
                        this.setData({
                                carts: res.data.data.items
                        })
                        this.price()
                })
        },

        // 加
        plus(e) {
                let {
                        number,
                        key
                } = e.currentTarget.dataset
                number++
                let obj = {
                        number,
                        key
                }

                getnumbers(obj).then(res => {
                        this.setData({
                                carts: res.data.data.items
                        })
                        this.price()
                })
        },

        // 减
        reduce(e) {
                let {
                        number,
                        key
                } = e.currentTarget.dataset
                number--
                let obj = {
                        number,
                        key
                }
                if (number <= 0) {
                        number = 1
                        getremove(obj).then(res => {
                                if (res.data.code === 700) {
                                        this.setData({
                                                carts: ""
                                        })
                                } else {
                                        this.setData({
                                                carts: res.data.data.items
                                        })
                                }
                                this.price()
                        })
                }

                getnumbers(obj).then(res => {
                        this.setData({
                                carts: res.data.data.items
                        })
                        this.price()
                })
        },

        // 去结算
        settlement() {
                let prc = this.data.price;
                let lzh = JSON.stringify(this.data.carts)
                wx.reLaunch({
                        url: `/pages/settlement/settlement?prc=${prc}&&lzh=${lzh}`
                })
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                let list = wx.getStorageSync('changeCart') || [];

                // 读数据
                getread(list).then(res => {
                        this.setData({
                                carts: res.data.data.items
                        })
                        this.price()
                })
        },

        goHome() {
                wx.switchTab({
                        url: '/pages/home/home',
                })
        },
        account(){
                wx.redirectTo({
                        url:'/pages/account/account'
                })
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
   navigate("navigateBack",{url:"/pages/shopCart/shopCart"},true)
        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function () {

        },

        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        onPullDownRefresh: function () {

        },

        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function () {

        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function () {

        }
})